﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Acms.Bll;
using Acms.DataControls;

public partial class Templates_Gallery_Default : Acms.AcmsControls.TemplatePage
{
    protected override void OnInit(EventArgs e)
    {
        base.OnInit(e);
        dsList.SelectData += new EventHandler<Acms.DataControls.SelectEventArgs<Acms.Bll.Post>>(dsList_SelectData);

        dlFeed.ItemDataBound += new EventHandler<Acms.WebControls.ItemDataBoundEventArgs>(dlFeed_ItemDataBound);

        dsSibling.SelectData += new EventHandler<Acms.DataControls.SelectEventArgs<Acms.Bll.Post>>(dsSibling_SelectData);
        dlNavSiblings.ItemDataBound += new EventHandler<Acms.WebControls.ItemDataBoundEventArgs>(dlNavSiblings_ItemDataBound);
        
        //dlFeed.ItemDataBound+= new EventHandler<Acms.WebControls.AcmsDataList>(dlFeed_ItemDataBound);
        //dlFeed.ItemDataBound += new EventHandler<Acms.WebControls.AcmsDataListItemEventArgs>(dlFeed_ItemDataBound); 

        //base.DataBind();
        //GetListData();
    }

    void dlFeed_ItemDataBound(object sender, Acms.WebControls.ItemDataBoundEventArgs e)
    {
        var post = e.DataItem as Post;
        if (post != null)
        {
            var lnkThumb = e.Item.FindControl("lnkThumb") as HyperLink;
            var lnkDownload = e.Item.FindControl("lnkDownload") as HyperLink;

            if (lnkThumb != null)
            {
                //lnk.Text = post.DisplayName;
                //lnk.NavigateUrl = post.Url;
                lnkThumb.ToolTip = post.DisplayName;

                var Image = post.FindPostControl("Image");// as ImageContentControlDisplay;
                if (Image != null)
                {
                    var prop = Image.GetProperties<Acms.AcmsControls.ImageContentControlProperties>();
                    if (prop != null && prop.HasResource())
                    {
                        lnkThumb.NavigateUrl = Acms.AcmsControls.RenderFile.GetUrl(prop.ResourceId, null, 550, this.Page);
                        lnkDownload.NavigateUrl = "http://www.schiebel.net/FTPfolder/Images/" + post.Name + ".zip";
                    }
                }

                //lnk.Attributes.Add("rel", "Next");

            }
        }
    }

    //void dlFeed_ItemDataBound(object sender, Acms.WebControls.AcmsDataListItemEventArgs e)
    //{
    //    var post = e.DataItem as Post;
    //    if (post != null)
    //    {
    //        var lnk = e.Item.FindControl("lnkNavSibling") as HyperLink;

    //        if (lnk != null)
    //        {
    //            lnk.Text = post.DisplayName;
    //            lnk.NavigateUrl = post.Url;
    //        }
    //    }
            
    //}

    //void dlFeed_ItemDataBound(object sender, Acms.WebControls.ItemDataBoundEventArgs e)
    //{
    //    var post = e.DataItem as Post;
    //    if (post != null)
    //    {
    //        var lnk = e.Item.FindControl("lnkNavSibling") as HyperLink;

    //        if (lnk != null)
    //        {
    //            lnk.Text = post.DisplayName;
    //            lnk.NavigateUrl = post.Url;
    //        }
    //    }
    //}

    protected void btnApplyFilter_Click(object sender, EventArgs e)
    {
        List<int> filters = new List<int>();
        List<Topic> products = Topic.GetAllBySiteIdAndTopicGroupPath(AcmsContext.CurrentPost.SiteId, "ImageProducts");
        for (int i = 0; i < cblProducts.Items.Length; i++)
        {
            if (cblProducts.Items[i].Checked)
            {
                filters.Add(products[i].Id);
            }
        }
        
        FilterIds = filters.ToArray();
        dlFeed.DataBind();
        //ucFeedModule.Rebind();
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        pnlCheckAllCheckBoxList.Visible = false;
        if (!IsPostBack)
        {
            DataBind();
            pnlCheckAllCheckBoxList.Visible = true;
        }
    }

    //private List<Post> listData;
    //protected List<Post> GetListData()
    //{
    //    if (listData == null)
    //    {
    //        OrgasiniseData();
    //    }
    //    return listData;
    //}

    int[] FilterIds;

    //private void OrgasiniseData()
    //{
    //    if (AcmsContext.CurrentPost != null)
    //    {
    //        listData = new List<Post>();
    //        //exploreData = new List<Post>();
    //        List<Post> children = null;
    //        if (FilterIds == null || FilterIds.Length == 0)
    //        {
    //            children = AcmsContext.CurrentPost.Children;
                
    //        }
    //        else if (FilterIds.Length == 1)
    //        {
    //            children = Post.GetAllByTopicIdAndParentPath(AcmsContext.SiteId, FilterIds[0], AcmsContext.CurrentPost.Path, AcmsContext.SiteVersion);
    //        }
    //        else
    //        {
    //            children = Post.GetAllByTopicIdsAndParentPath(AcmsContext.SiteId, FilterIds, AcmsContext.CurrentPost.Path, AcmsContext.SiteVersion);
    //        }
    //        foreach (Post p in children)
    //        {
    //            if (p.TemplateId == AcmsContext.CurrentPost.TemplateId)
    //            {
    //                listData.Add(p);
    //                //exploreData.Add(p);
    //            }
    //            else
    //            {
    //                listData.Add(p);
    //            }
    //        }
    //    }
    //    else
    //    {
    //        //exploreData = DummyPost.CreateDummyList(5);
    //        listData = DummyPost.CreateDummyList(12);
    //    }
    //}
    
    void dsList_SelectData(object sender, Acms.DataControls.SelectEventArgs<Acms.Bll.Post> e)
    {
        //e.SetData(Post.GetAllBySiteIdAndParentPath(AcmsContext.SiteId, AcmsContext.CurrentPost.Path, AcmsContext.SiteVersion));
        //e.SetData(GetListData());
        List<Post> data = null;
        
        if (FilterIds == null || FilterIds.Length == 0) {
            if (txtFirstFilter.Value == "0")
            {
                data = AcmsContext.CurrentPost.Children;
                txtFirstFilter.Value = "1";
            }
            else
            {
                data = null;
            }
        }
        else if (FilterIds.Length == 1)
        {
            data= Post.GetAllByTopicIdAndParentPath(AcmsContext.SiteId, FilterIds[0], AcmsContext.CurrentPost.Path, AcmsContext.SiteVersion);
        }
        else 
        {
            data= Post.GetAllByTopicIdsAndParentPath(AcmsContext.SiteId, FilterIds, AcmsContext.CurrentPost.Path, AcmsContext.SiteVersion);
        }
        
        e.SetData(data);

    }

    public void Rebind()
    {
        dlFeed.DataBind();
    }

    protected override void DataBindChildren()
    {
        cblProducts.DataSource = Topic.GetAllBySiteIdAndTopicGroupPath(AcmsContext.SiteId, "ImageProducts");
        //accordianFilterPane.DataBind();
        base.DataBindChildren();
    }

    void dsSibling_SelectData(object sender, Acms.DataControls.SelectEventArgs<Acms.Bll.Post> e)
    {
        e.SetData(AcmsContext.CurrentPost.Parent.Children.OrderBy(x => x.Sequence).ToList());
    }
    void dlNavSiblings_ItemDataBound(object sender, Acms.WebControls.ItemDataBoundEventArgs e)
    {
        var post = e.DataItem as Post;
        if (post != null)
        {
            var lnk = e.Item.FindControl("lnkNav") as HyperLink;

            if (lnk != null)
            {
                lnk.Text = post.DisplayName;
                lnk.NavigateUrl = post.Url;
            }
        }
    }


}
